#include "LevelMenuState.h"

#include "../SGD Wrappers/SGD_String.h"

#include "BitmapFont.h"
#include "MainMenuState.h"
#include "GamePlayState.h"
#include "Game.h"
#include "ProfileManager.h"
#include "AudioManager.h"


LevelMenuState* LevelMenuState::pInstance = nullptr;
/*static*/ LevelMenuState* LevelMenuState::GetInstance( void )
{
	// Allocate the static pointer if necessary
	if( pInstance == nullptr )
		pInstance = new LevelMenuState;

	// Return the singleton
	return pInstance;
}
/*static*/ void LevelMenuState::DeleteInstance( void )
{
	delete pInstance;
	pInstance = nullptr;
}


// Update
void LevelMenuState::Update(float deltaTime)
{
	if(controllerDelay > 0.0f)
		controllerDelay -= deltaTime;
	if(dI->KeyPressed(DIK_ESCAPE) || dI->JoystickButtonPressed(6) || dI->JoystickButtonPressed(7))
	{
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
		return;
	}

	if(dI->KeyPressed(DIK_UP) || dI->KeyPressed(DIK_W) || dI->JoystickDPadPressed(DIR_UP) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() < 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.17f;
		if(cursory > 0)
			--cursory;
		else
			cursory = 4;
	}
	else if (dI->KeyPressed(DIK_DOWN) || dI->KeyPressed(DIK_S) || dI->JoystickDPadPressed(DIR_DOWN) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() > 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.25f;
		if(cursory < 4)
			++cursory;
		else
			cursory = 0;
	}
	if(dI->KeyPressed(DIK_LEFT) || dI->KeyPressed(DIK_A) || dI->JoystickDPadPressed(DIR_LEFT) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() < 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.25f;
		if(cursorx > -1)
			--cursorx;
		else
			cursorx = 4;
	}
	if(dI->KeyPressed(DIK_RIGHT) || dI->KeyPressed(DIK_D) || dI->JoystickDPadPressed(DIR_RIGHT) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() > 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.25f;
		if(cursorx < 4)
			++cursorx;
		else
			cursorx = -1;
	}

	if(dI->KeyPressed(DIK_RETURN) || dI->JoystickButtonPressed(0))
	{
		if(cursorx == -1)
		{
			Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
			return;
		}
		else
		{
			//TODO:
			//Tell Levelmanager which level to load
			int tempX = 0, tempY = 0;
			//switch(cursorx)
			//{
			//case 0:
			//	tempX = 4;
			//	break;
			//case 1:
			//	tempX = 3;
			//	break;
			//case 2:
			//	tempX = 2;
			//	break;
			//case 3:
			//	tempX = 1;
			//	break;
			//case 4:
			//	tempX = 0;
			//	break;
			//}
			switch(cursory)
			{
			case 0:
				tempY = 4;
				break;
			case 1:
				tempY = 3;
				break;
			case 2:
				tempY = 2;
				break;
			case 3:
				tempY = 1;
				break;
			case 4:
				tempY = 0;
				break;
			}
			int tempLevel = cursorx+ tempY *5;
			if(tempLevel <= ProfileManager::GetInstance()->getCurProfile().getLevelsUnlocked() + 1)
			{
				GamePlayState::GetInstance()->Enter(tempLevel);
				Game::GetInstance()->ChangeState(GamePlayState::GetInstance());

				//Change State to GameplayState
				AudioManager::GetInstance()->switchMusic("menus","gameplay");
			}
		}
	}
}

// Render
void LevelMenuState::Render(void) const
{

	//Render the Background
	if( BackgroundImageID != -1 )
	{
		tM->Draw(BackgroundImageID, 0,0,0.95f,1.055f);
	}
	RECT renderRect;
	ostringstream toss;

	//Frame area width and height

	//Temp Draw
	RECT FrameSizeRect = {175, 30, 175 + 126 * 6, 30 + 126* 6};
	//D3D->DrawRect(FrameSizeRect, D3DCOLOR_ARGB(255,0,40,180));

	int NavBoxWidth = 756 / 2;
	int NavBoxHeight = 756 / 2;

	int GameHeight = Game::GetInstance()->GetScreenHeight();
	int GameWidth = Game::GetInstance()->GetScreenWidth();

	//Offset the Rect
	OffsetRect(&FrameSizeRect, GameWidth / 2 - NavBoxWidth - 175, GameHeight / 2 - NavBoxHeight - 30);

	//int BorderHeight = FrameSizeRect.bottom - FrameSizeRect.top;
	//int BorderWidth = FrameSizeRect.right - FrameSizeRect.left;

	if( MenuFrameID != -1 )
		tM->Draw(MenuFrameID, FrameSizeRect.left, FrameSizeRect.top, 1.0f,1.0f, 0, 0.0f,0.0f,0.0f, D3DCOLOR_ARGB(160,255,255,255));
	//tM->Draw(MenuFrameID, 175, 30);

	for(int width = 0; width < 5; ++width)
	{
		for(int height = 0; height < 5; ++height)
		{
			renderRect.top	 = 50 + 126*height + 20*height;
			renderRect.bottom = 140 + 126*height + 20*height;
			renderRect.left	 = 200 + 126*width + 20*width; 
			renderRect.right = 290 + 126*width + 20*width;

			//D3D->DrawRect(renderRect,D3DCOLOR_ARGB(255,255,0,0));
			renderRect.left *= Scale;
			renderRect.top *= Scale;

			//Offset after scale
			if( Scale != 1.0f )
			{
				OffsetRect(&renderRect,75,50);
			}

			//Offset each rect to cent
			OffsetRect( &renderRect, GameWidth / 2 - NavBoxWidth - 175, GameHeight / 2 - NavBoxHeight - 30);

			//Draw the Level Boxes

			//if(width*4 + height >= ProfileManager::GetInstance()->getCurProfile().getLevelsUnlocked())
			//{
			//Draw the unlocked Level button
			if( ActiveButtonID != -1 )
			{
				tM->Draw(ActiveButtonID, renderRect.left, renderRect.top);
			}
			//}
			//else
			//{


			//}

			/*int levelsUnlocked = ProfileManager::GetInstance()->getCurProfile().getLevelsUnlocked();
			int num1, num2;
			num2 = width+1;
			switch(height)
			{
			case 0:
			num1 = (height+1)+4;
			toss << num1 << " . " << num2;
			break;					    
			case 1:		
			num1 = (height+1)+2;
			toss << num1 << " . " << num2;
			break;					    	
			case 2:		
			num1 = height+1;
			toss << num1 << " . "   << num2;
			break;					    	 
			case 3:				


			num1 = (height+1)-2;
			toss << num1 << " . " << num2;
			break;					    	 
			case 4:					 
			num1 = (height+1)-4;
			toss << num1 << " . " << num2;
			break;
			}

			BitmapFont::GetInstance()->Print(toss.str().c_str(), (float)renderRect.left + 33, (float)renderRect.top + 50 ,0, D3DCOLOR_ARGB(255,0,0,0));
			toss.str("");*/

			/*	int levelSelected =width + height*4;*/
			//TODO: MAKE THIS WORK
			/*		if(levelSelected <= ProfileManager::GetInstance()->getCurProfile().getLevelsUnlocked())
			D3D->DrawRect(renderRect,D3DCOLOR_ARGB(170,0,0,0));*/

		}
	}
	//Darken locked levels
	for(int height = 0; height < 5; ++height)
	{
		for(int width = 0; width < 5; ++width)
		{
			renderRect.top	 = 635 - 126*height - 20*height;
			renderRect.bottom = 580 - 126*height - 20*height;
			renderRect.left	 = 200+ 126*width + 20*width; 
			renderRect.right = 290 + 126*width + 20*width;

			renderRect.left *= Scale;
			renderRect.top *= Scale;

			//Offset each rect to cent
			OffsetRect( &renderRect, GameWidth / 2 - NavBoxWidth - 175, GameHeight / 2 - NavBoxHeight - 30);

			if( Scale != 1.0f )
			{
				OffsetRect(&renderRect,75,50);
			}


			if((height*4 + width) >= ProfileManager::GetInstance()->getCurProfile().getLevelsUnlocked() + 1)
			{

				//Draw the locked level button
				if( LockedButtonID != -1 )
				{
					tM->Draw(LockedButtonID, renderRect.left, renderRect.top);
				}
			}
		}
	}


	renderRect.top = Game::GetInstance()->GetScreenHeight()- 150;
	renderRect.bottom = Game::GetInstance()->GetScreenHeight()-120;
	renderRect.left = 25;
	renderRect.right = 150;

	renderRect.left *= Scale;
	renderRect.top *= Scale;

	if( Scale != 1.0f )
	{
		//	OffsetRect(&renderRect,75,50);
	}

	//Draw back button
	if( BackButtonID != -1 && Scale != 1.0f)
	{
		tM->Draw(BackButtonID, (renderRect.left - 20), renderRect.top + 150, 0.3f, 0.3f);
	}
	else if( BackButtonID != -1 )
	{
		tM->Draw(BackButtonID, renderRect.left, renderRect.top);
	}




	RECT selectedRect;

	if(cursorx != -1)
	{
		selectedRect.top	 = 50 + 126*cursory + 20*cursory;
		selectedRect.bottom = 140 + 126*cursory + 20*cursory;
		selectedRect.left	 = 200+ 126*cursorx + 20*cursorx; 
		selectedRect.right = 290 +  126*cursorx + 20*cursorx;

		selectedRect.left *= Scale;
		selectedRect.top *= Scale;

		if( Scale != 1.0f )
		{
			OffsetRect(&selectedRect,75,50);
		}

		OffsetRect( &selectedRect, GameWidth / 2 - NavBoxWidth - 175, GameHeight / 2 - NavBoxHeight - 30);

		if(CursorImageID != -1)
			tM->Draw( CursorImageID, selectedRect.left, selectedRect.top);

	}
	else
	{
		//selectedRect.top = Game::GetInstance()->GetScreenHeight()-100;
		//selectedRect.bottom = Game::GetInstance()->GetScreenHeight()-20;
		//selectedRect.left = 5;
		//selectedRect.right = 150;

		if( HighlightedBackButtonID != -1 && Scale != 1.0f )
			tM->Draw( HighlightedBackButtonID, renderRect.left - 20, renderRect.top + 150, 0.3f, 0.3f );
		else if ( HighlightedBackButtonID != -1 )
			tM->Draw( HighlightedBackButtonID, renderRect.left, renderRect.top);

	}

	//D3D->DrawRect(renderRect,D3DCOLOR_ARGB(255,255,0,0));
	if( Scale == 1.0f )
		BitmapFont::GetInstance()->Print("Back", (float)renderRect.left + 85, (float)renderRect.top + 50 ,0, D3DCOLOR_ARGB(255,0,0,0));
	else
		BitmapFont::GetInstance()->Print("Back", (float)renderRect.left - 15, (float)renderRect.top + 160 ,0, D3DCOLOR_ARGB(255,0,0,0), 0.8f);



	//D3D->DrawHollowRect(selectedRect,D3DCOLOR_ARGB(255,255,255,0),3);

	//Render Text	
	for(int width = 0; width < 5; ++width)
	{
		for(int height = 0; height < 5; ++height)
		{
			renderRect.top	 = 50 + 126*height + 20*height;
			renderRect.bottom = 140 + 126*height + 20*height;
			renderRect.left	 = 200 + 126*width + 20*width; 
			renderRect.right = 290 + 126*width + 20*width;

			renderRect.left *= Scale;
			renderRect.top *= Scale;

			if( Scale != 1.0f )
			{
				OffsetRect(&renderRect,75,50);
			}

			//D3D->DrawRect(renderRect,D3DCOLOR_ARGB(255,255,0,0));

			//Offset each rect to cent
			OffsetRect( &renderRect, GameWidth / 2 - NavBoxWidth - 175, GameHeight / 2 - NavBoxHeight - 30);

			int levelsUnlocked = ProfileManager::GetInstance()->getCurProfile().getLevelsUnlocked();
			int num1, num2;
			num2 = width+1;
			switch(height)
			{
			case 0:
				num1 = (height+1)+4;
				toss << num1 << " . " << num2;
				break;					    
			case 1:		
				num1 = (height+1)+2;
				toss << num1 << " . " << num2;
				break;					    	
			case 2:		
				num1 = height+1;
				toss << num1 << " . "   << num2;
				break;					    	 
			case 3:				


				num1 = (height+1)-2;
				toss << num1 << " . " << num2;
				break;					    	 
			case 4:					 
				num1 = (height+1)-4;
				toss << num1 << " . " << num2;
				break;
			}

			BitmapFont::GetInstance()->Print(toss.str().c_str(), (float)renderRect.left + 33, (float)renderRect.top + 50 ,0, D3DCOLOR_ARGB(255,0,0,0));
			toss.str("");

		}

	}
}

// Enter
//	- Used to initialize the game state.
void LevelMenuState::Enter(void)
{
	GameState::Enter();

	ActiveButtonID = tM->LoadTexture("Textures\\Menu\\LevelSelButtonActive.png");
	MenuFrameID = tM->LoadTexture("Textures\\Menu\\LevelSelectNavBox.png");
	BackgroundImageID =  tM->LoadTexture("Textures\\Menu\\LevelSelectBackground.jpg");
	CursorImageID = tM->LoadTexture("Textures\\Menu\\LevelSelButtonCursor.png");
	BackButtonID = tM->LoadTexture("Textures\\Menu\\LevelSelBackButton.png");
	HighlightedBackButtonID = tM->LoadTexture("Textures\\Menu\\LevelSelButtonBackHighLight.png");
	LockedButtonID =  tM->LoadTexture("Textures\\Menu\\LevelSelButtonLocked.png");
}

// Exit
//	- Used to clean up the game state.
void LevelMenuState::Exit(void)
{
	GameState::Exit();
	DeleteInstance();
}


LevelMenuState::LevelMenuState(void)
{
	cursorx = 0;
	cursory = 0;

	ActiveButtonID = -1;
	InactiveButtonID = -1;
	MenuFrameID = -1;
	BackgroundImageID = -1;
	CursorImageID = -1;
	BackButtonID = -1;
	HighlightedBackButtonID = -1;
	LockedButtonID = -1;

	if( Game::GetInstance()->GetScreenWidth() == 800 && Game::GetInstance()->GetScreenHeight() == 600 )
	{
		LevelMenuState::Scale = 0.85f;
	}
	else if( Game::GetInstance()->GetScreenWidth() != 800 && Game::GetInstance()->GetScreenHeight() != 600) 
	{
		LevelMenuState::Scale = 1.0f;
	}


}
LevelMenuState::~LevelMenuState(void)
{

}